package com.hrm.company.controller;

import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.company.service.CompanyService;
import com.hrm.domain.company.Company;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/company")
@Api(value = "/company", tags = "<code>企业相关的接口</code>")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    // 保存企业
    @ApiOperation(value = "保存企业")
    @PostMapping("")
    public Result save(@RequestBody @ApiParam(value = "企业对象") Company company) {
        companyService.add(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据id更新企业
    @ApiOperation(value = "根据id更新企业")
    @PutMapping("/{id}")
    public Result update(@PathVariable @ApiParam(value = "id") String id,
                         @RequestBody @ApiParam(value = "企业对象") Company company) {
        company.setId(id);
        companyService.update(company);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据id删除企业
    @ApiOperation(value = "根据id删除企业")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable @ApiParam(value = "id") String id) {
        companyService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据id查询企业
    @ApiOperation(value = "根据id查询企业")
    @GetMapping("/{id}")
    public Result findById(@PathVariable @ApiParam(value = "id") String id) {
        Company company = companyService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(company);
        return result;
    }

    // 查询全部企业列表
    @ApiOperation(value = "查询全部企业列表")
    @GetMapping("")
    public Result findAll() {
        List<Company> list = companyService.findAll();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return result;
    }
}
